package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.bean.PopupWindowBean;
import com.zhuozhou.jdyc.util.log.LogUtil;
import com.zhuozhou.jdyc.wight.pickertime.PickValueView;

import java.util.List;

/**
 * Created by 方舟 on 2017/10/18.
 * 单选弹框
 */

public class PickStringDialog implements View.OnClickListener, PickValueView.onSelectedChangeListener, TextWatcher {

    public Context context;
    private Dialog customDialog;
    private int type = 0;
    private TextView submitBtn, tvSelected, cancel, submit;
    private View pickerView;
    private EditText searchEdit;
    private FrameLayout frame1, frame2;
    private LinearLayout pvLayout, search;
    private PickValueView pickString;
    private OnPickSelectedListener onPickSelectedListener;
    private OnSearchEditListener onSearchEditListener;
    private String defaultString;
    private List<PopupWindowBean> details;

    public PickStringDialog(Context context, List<PopupWindowBean> details, OnPickSelectedListener onPickSelectedListener) {
        this.context = context;
        this.details = details;
        this.onPickSelectedListener = onPickSelectedListener;
    }

    public PickStringDialog(Context context, int type, List<PopupWindowBean> details, OnPickSelectedListener onPickSelectedListener) {
        this.context = context;
        this.type = type;
        this.details = details;
        this.onPickSelectedListener = onPickSelectedListener;
    }

    public PickStringDialog setOnSearchEditListener(OnSearchEditListener onSearchEditListener) {
        this.onSearchEditListener = onSearchEditListener;
        return this;
    }

    public PickStringDialog setDetails(List<PopupWindowBean> details) {
        this.details = details;
        initPickData();
        return this;
    }

    public Dialog create(String defaultString) {
        this.defaultString = defaultString;

        customDialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        pickerView = LayoutInflater.from(context).inflate(
                R.layout.numberpicker, null);
        pvLayout = (LinearLayout) pickerView.findViewById(R.id.Main_pvLayout);
        tvSelected = (TextView) pickerView.findViewById(R.id.Main_tvSelected);
        cancel = (TextView) pickerView.findViewById(R.id.cancel);
        submit = (TextView) pickerView.findViewById(R.id.submit);
        pickString = (PickValueView) pickerView.findViewById(R.id.pickString);
        frame1 = pickerView.findViewById(R.id.frame1);
        frame2 = pickerView.findViewById(R.id.frame2);
        search = pickerView.findViewById(R.id.search);
        searchEdit = pickerView.findViewById(R.id.searchEdit);
        pickString.setVisibility(View.VISIBLE);

        if (type == 1) {
            frame1.setVisibility(View.GONE);
            frame2.setVisibility(View.VISIBLE);
        } else if (type == 2) {
            search.setVisibility(View.VISIBLE);
            frame1.setVisibility(View.GONE);
            frame2.setVisibility(View.VISIBLE);
            searchEdit.addTextChangedListener(this);
        }
        if (defaultString == null || defaultString.equals("")) ;
        else tvSelected.setText(defaultString);

        pickString.setOnSelectedChangeListener(this);

        submitBtn = (TextView) pickerView.findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);
        cancel.setOnClickListener(this);
        submit.setOnClickListener(this);
        initPickData();
        customDialog.setContentView(pickerView); // 将布局设置给Dialog
        Window dialogWindow = customDialog.getWindow();
        dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);// 设置dialog宽度
        dialogWindow.setGravity(Gravity.BOTTOM);
        return customDialog;
    }

    private void initPickData() {
        if (details == null) return;
        String[] strs = new String[details.size()];
        for (int i = 0; i < details.size(); i++) {
            strs[i] = details.get(i).getName();
        }
        if (strs.length < 3)
            pickString.setShowCount(strs.length);

        if (defaultString == null || defaultString.equals("")) {
            pickString.setValueData(strs, strs.length < 1 ? "" : strs[0]);
        } else {
            pickString.setValueData(strs, defaultString);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.submit:
            case R.id.submitBtn:
                if (customDialog != null && customDialog.isShowing()) customDialog.dismiss();
                if (onPickSelectedListener != null) {
//                    LogUtil.show("走if：" + TextUtils.isEmpty(tvSelected.getText().toString()));
                    if (TextUtils.isEmpty(tvSelected.getText().toString())) {
                        for (int i = 0; i < details.size(); i++) {
                            if (details.get(i).getName().equals(pickString.getLeftDefaultString()))
                                onPickSelectedListener.pickSelected(this,
                                        details.get(i).getIndex(), pickString.getLeftDefaultString());
                        }
                    } else {
                        onPickSelectedListener.pickSelected(this,
                                (Integer) tvSelected.getTag(), tvSelected.getText().toString());
                    }
                }
                break;

            case R.id.cancel:
                customDialog.dismiss();
                break;
        }
    }

    @Override
    public void onSelected(PickValueView view, Object leftValue, Object middleValue, Object rightValue) {
        if (view == pickString && leftValue != null) {
            tvSelected.setText(leftValue.toString());
            for (PopupWindowBean contents : details) {
                if (contents.getName().equals(leftValue.toString()))
                    tvSelected.setTag(contents.getIndex());
            }
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (onSearchEditListener != null)
            onSearchEditListener.OnSearchEdit(s.toString());
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public interface OnPickSelectedListener {
        void pickSelected(PickStringDialog mCurrentPick, Integer position, String showText);
    }

    public interface OnSearchEditListener {
        void OnSearchEdit(String showText);
    }
}
